﻿using System;
using System.Web.UI.WebControls;
using DotNetNuke.Entities.Tabs;
using DotNetNuke.UI.Skins;
using System.Web.UI;
namespace DotNetNuke.Modules.SkinObjectsEx
{
	/// -----------------------------------------------------------------------------
	/// <summary></summary>
	/// <returns></returns>
	/// <remarks></remarks>
	/// <history>
	/// 	[brc]	12/04/2006	new skin object for a single page link
    /// 	[brc]   22/12/2011  convert for c#/ new SkinObjectEx project
	/// </history>
	/// -----------------------------------------------------------------------------

	public class PageLink : UI.Skins.SkinObjectBase
	{

		// private members
		//css Class for changing style
		private string _cssClass = "";
		//name of page within the site
		private string _pageName = "";
		//the url to the page
		private string _url = "";
		//the text to show on the page
		private string _linkCaption = "";
		//anchor target
		private string _target = "";
		//rel='nofollow'
		private bool _useNoFollow = false;

		const string MyFileName = "PageLink.ascx";
		// protected controls

		#region "Protected Controls"
			#endregion
		protected Label lblPageLink;

		#region "Public Members"
		public string CssClass {
			get { return _cssClass; }
			set { _cssClass = value; }
		}

		public string PageName {
			get { return _pageName; }
			set { _pageName = value; }
		}

		public string URL {
			get { return _url; }
			set { _url = value; }
		}
		public string Target {
			get { return _target; }
			set { _target = value; }
		}
		public string LinkCaption {
			get { return _linkCaption; }
			set { _linkCaption = value; }
		}
		public bool NoFollow {
			get { return _useNoFollow; }
			set { _useNoFollow = value; }
		}
		#endregion

		#region " Web Form Designer Generated Code "


		[System.Diagnostics.DebuggerStepThrough()]

		private void InitializeComponent()
		{
		}

		private void Page_Init(System.Object sender, System.EventArgs e)
		{
			//CODEGEN: This method call is required by the Web Form Designer
			//Do not modify it using the code editor.
			InitializeComponent();
		}

		#endregion

		//*******************************************************
		//
		// The Page_Load server event handler on this page is used
		// to populate the role information for the page
		//
		//*******************************************************


		private void Page_Load(System.Object sender, System.EventArgs e)
		{

			// public attributes
			string cssClass = null;
			string linkCaption = null;
			string linkUrl = null;
			//initialise defaults
			if (!string.IsNullOrEmpty(CssClass)) 
            {
			    cssClass = CssClass;
			} 
            else 
            {
				cssClass = "SkinObject";
			}
			//caption set in following order:
			// 1. Caption Property
			// 2. Page Name Property
			// 3. URL property
            if (!string.IsNullOrEmpty(_linkCaption))
            {
                if (_linkCaption.Length > 0)
                {
                    linkCaption = _linkCaption;
                }
                else
                {
                    if (string.IsNullOrEmpty(_pageName) == false || _pageName.Length == 0)
                    {
                        if (_url.Length > 0)
                        {
                            linkCaption = _url;
                        }
                    }
                    else
                    {
                        linkCaption = _pageName;
                    }
                }
            }
			//get the URL of the page 
			linkUrl = GetPageLink();
			if (linkUrl.Length == 0) {
				//format with the specified url
				linkUrl = _url;
			}
			string noFollow = "";
			if ((_useNoFollow == true)) {
				noFollow = " rel='nofollow'";
			}
			//build the Anchor tag with the specified options
			if (linkUrl.Length > 0) 
            {
				lblPageLink.CssClass = cssClass;
				lblPageLink.Text = "<a href=\"" + linkUrl + "\"" + (_target.Length > 0 ? "target=\"" + _target + "\"" : "") + noFollow + ">" + linkCaption + "</a>";
			} 
            else 
            {
				lblPageLink.Text = "<span class=\"" + linkCaption + "\"</span>";
			}

		}
		private string GetPageLink()
		{
			//does : retrieves the specific page URL for the specified page
			//gives : a URL to the page
			//
			string pageURL = "";
            TabController tc = new TabController();
            TabInfo tab = tc.GetTabByName(_pageName, this.PortalSettings.PortalId);
            if (tab != null)
            {
                if (tab.IsDeleted == false
                && ((tab.StartDate < DateTime.Now 
                && tab.EndDate > DateTime.Now) || AdminMode == true))
                {
                    if (DotNetNuke.Security.PortalSecurity.IsInRoles(tab.AuthorizedRoles) == true) 
                    {
						//this is the tab specified
						pageURL = DotNetNuke.Common.Globals.NavigateURL(tab.TabID);
                    }
                }
            }
			return pageURL;
		}
		public PageLink()
		{
			Load += Page_Load;
			Init += Page_Init;
		}
	}

}
